<?php
/**
 * Loads all worlds.
 *
 * @author Mark Dohmen.
 */
final class WorldLoader {
	
	/**
	 * Instance of WorldLoader.
	 */
	private static $worldLoader = NULL;
	
	/**
	 * A list of world information.
	 */
	private $worldList;
	
	/**
	 * Load all worlds.
	 */
	final public function load() {
		Database::getDatabase()->connect(Constants::DB_NAME);
		$data = Database::getDatabase()->fetchArray("SELECT * FROM `worlds`");
		foreach ($data as $world) {
			$this->worldList[$world['id']] = $world['database_name'];
		}
	}
	
	/**
	 * Loads the player data from the database
	 * (from the specific worlds database to say),
	 * and registers the players with the world.
	 */
	final public function loadPlayers() {
		$data = Database::getDatabase()->fetchArray("SELECT * FROM `accounts`");
		foreach ($data as $playerData) {
			$player = new Player(new PlayerDefinition());
			World::getWorld()->registerPlayer($player);
		}
	}
	
	/**
	 * Gets the instance of WorldLoader.
	 *
	 * @return $worldLoader/null
	 */
	final public static function getWorldLoader() {
		if (!(self::$worldLoader instanceof WorldLoader)) {
			self::$worldLoader = new WorldLoader();
		}
		return (object) self::$worldLoader;
	}
	
	/**
	 * Gets the world list.
	 *
	 * @return @worldList
	 */
	final public function getWorldList() {
		return (array) $this->worldList;	
	}
	
	/**
	 * Get a world by ID.
	 *
	 * @param $id
	 * @return $worldList[$id]
	 */
	final public function getWorldByID(int $id) {
		return (string) $this->worldList[$id];
	}
}
?>